package com.blue.gateway.paycore.alipay.pay;

import com.alibaba.fastjson.JSON;
import com.blue.gateway.config.BaseChannelConfig;
import com.blue.gateway.annotation.Channel;
import com.blue.gateway.common.request.PayRequest;
import com.blue.gateway.common.Request;
import com.blue.gateway.config.AliConfig;
import com.blue.gateway.paycore.alipay.AliPayConstants;
import com.blue.gateway.paycore.alipay.AliMethodEnum;
import com.blue.gateway.dto.Result;
import com.blue.gateway.enums.PayChannelEnum;
import com.blue.gateway.enums.ServiceChannelEnum;
import com.blue.gateway.utils.DateUtil;
import com.blue.gateway.utils.WebUtil;
import com.blue.gateway.utils.sign.AliPaySignature;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 * @author xiaoyiming
 * @date 2020/5/4 14:43
 * @depiction https://opendocs.alipay.com/apis/api_1/alipay.trade.wap.pay
 */
@Slf4j
@Channel(PayChannel = PayChannelEnum.ALIPAY_H5, ServiceChannel = ServiceChannelEnum.PAY)
public class AliPayH5Service extends AliPayService{
    @Override
    public Result validate(Request request) {
        return super.validate(request);
    }

    @Override
    public BaseChannelConfig getConfig(Request request) {
        return super.getConfig(request);
    }

    @Override
    public String generateParams(Request request, BaseChannelConfig channelConfig) {
        PayRequest payRequest = (PayRequest) request;
        AliConfig aliConfig = (AliConfig) channelConfig;
        //公共请求参数
        SortedMap<String, String> paramMap = new TreeMap<>();
        paramMap.put("app_id", aliConfig.getAppId());
        paramMap.put("method", AliMethodEnum.TRADE_WAP_PAY.getMethodName());
        paramMap.put("charset", AliPayConstants.CHARSET_UTF8);
        paramMap.put("sign_type", aliConfig.getSignType());
        paramMap.put("timestamp", DateUtil.formatDateYMDHMS(new Date()));
        paramMap.put("version", AliPayConstants.VERSION);
        paramMap.put("notify_url", payRequest.getNotifyUrl());
        if (StringUtils.isNotBlank(payRequest.getReturnUrl())) {
            paramMap.put("return_url", payRequest.getReturnUrl());
        }

        //请求参数 biz_content
        SortedMap<String, String> contentMap = new TreeMap<>();
        contentMap.put("out_trade_no", payRequest.getOutTradeNo());
        contentMap.put("total_amount", payRequest.getTotalAmount().toString());
        contentMap.put("subject", payRequest.getSubject());

        paramMap.put("biz_content", JSON.toJSONString(contentMap));

        //参数加密
        String sign = AliPaySignature.sign(paramMap, aliConfig.getPrivateKey());
        paramMap.put("sign", sign);

        return AliPayConstants.GATEWAY_URL + WebUtil.getRequestUrl(paramMap);
    }

    @Override
    public String execute(String requestParams, BaseChannelConfig channelConfig) {
        return super.execute(requestParams, channelConfig);
    }

    @Override
    public Result analysisResult(String executeResult) {
        return super.analysisResult(executeResult);
    }
}
